/**
 * Created by dbwyxl on 2017/9/19.
 */
module.exports =function($cookies,$scope, $rootScope, $state, contractLibraryService){

  $scope.tempList = [];
  $scope.menuList = [];
  //总共的收藏数目
  $scope.totalNum = 0;
  //栏目选中状态
  $scope.selLi = '0';

  $scope.contractLi={
    pageSize:10,
    beginNum:1,
    total:'',
    typeName:'',
    parentName:''
  }

  //下载附件
  $scope.downloadFileProgress = function (file, key) {
    /*taskId
     taskAttachedId*/
    var params = {'contractId':file.id, 'fileName':file.contractName};
    contractLibraryService.tempDownload(params).then(function (response) {
      var fileData = new Blob([response.data]);
      var link = document.createElement('a');
      link.href = URL.createObjectURL(fileData);
      link.download = file.contractName;
      link.click();
      // window.URL.revokeObjectURL(link.href);
    });

    this.tempList[key].downloadCount++;

  };




  //获取列表
  $scope.findUserTemplateCollectionList = function () {
    var params={
      pageSize:$scope.contractLi.pageSize,
      beginNum:$scope.contractLi.beginNum,
      typeName:$scope.contractLi.typeName,
      parentName:$scope.contractLi.parentName
    }
    contractLibraryService.findUserTemplateCollectionList(params).then(function (response) {
      var data = response.data;
      if(data.code == 1) {
        $scope.tempList = data.data;
        $scope.contractLi.total=data.total;
      }
    }, function (response) {

    });

  }

  //获取统计
  $scope.findUserTemplateCollectStatistics = function () {

    contractLibraryService.findUserTemplateCollectStatistics().then(function (response) {
      var data = response.data;
      if(data.code == 1) {
        $scope.menuList = data.data;
        for (var i=0; i<$scope.menuList.length; i++) {
          $scope.totalNum = $scope.totalNum+$scope.menuList[i].count;
          $scope.menuList[i].isckd = false;
        }
      }
    }, function (response) {

    });

  }

  $scope.init = function () {
    $scope.findUserTemplateCollectionList();
    $scope.findUserTemplateCollectStatistics();
  }

  $scope.init();


  //分页
  $scope.getContractLibrary = function () {
    $scope.findUserTemplateCollectionList();
  }

  //取消收藏
  $scope.cancelCollect = function (event) {

    // var params={id:id}
    /*contractLibraryService.deleteUserTemplateCollect(params).then(function (response) {
      var data = response.data;
      if(data.code == 1) {

      }
    }, function (response) {
      console.log('fail');
    });*/
  }

  //显示自己栏目
  $scope.showSubMenu = function (index) {
    $scope.menuList[index].isckd = !$scope.menuList[index].isckd;
  }

  //选择对应分类
  $scope.selectMenu = function (name, ck, pk) {

    $scope.selLi = pk+"-"+ck;
    $scope.contractLi.typeName = name;
    $scope.findUserTemplateCollectionList();
  }


  //取消收藏
  $scope.delCollect = function (obj, index) {
    var params={'templateId':obj.id}
    contractLibraryService.deleteUserTemplateCollect(params).then(function (response) {
      var data = response.data;
      if(data.code == 1) {
        $scope.tempList.splice(index,1);
      }
    }, function (response) {

    });
  }


};
